package com.jwthhealth_pub.old.individual.view;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.old.api.ApiHelper;
import com.jwthhealth_pub.old.individual.module.HeartData;
import com.jwthhealth_pub.old.individual.module.HeartDetailData;
import com.jwthhealth_pub.old.individual.view.adapter.HeartAdapter;
import com.jwthhealth_pub.old.sign.model.LoginModel;
import com.jwthhealth_pub.old.signutil.model.db.UserInfoDao;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/3/24.
 */

public class IndividualHeartActivity extends Activity {
    @BindView(R.id.sign_topbar)
    TitleLayout signTopbar;
    @BindView(R.id.btn_more)
    ImageView btnMore;
    @BindView(R.id.date_value)
    TextView dateValue;
    @BindView(R.id.rv_date)
    RecyclerView rvDate;
    @BindView(R.id.rv_date_two)
    RecyclerView rvDateTwo;
    @BindView(R.id.progressbar)
    ProgressBar progressbar;
//    @BindView(R.id.describe_layout)
//    RelativeLayout describeLayout;
    @BindView(R.id.tv_describe_value)
    TextView tvDescribeValue;
    @BindView(R.id.tv_case_value)
    TextView tvCaseValue;
    @BindView(R.id.mode_value)
    TextView modeValue;
    @BindView(R.id.rhythm_value)
    TextView rhythmValue;
    @BindView(R.id.complete_value)
    TextView completeValue;
    @BindView(R.id.fv_value)
    TextView fvValue;
    @BindView(R.id.fc_value)
    ImageView fcValue;
    @BindView(R.id.info_layout)
    View infoLayout;
    private LoginModel.DataBean userinfoModel;
    private int dataSize;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_individual_heart);
        ButterKnife.bind(this);

        initTop();
        initUI();
//        getData();
    }

    private void initUI() {
        HeartData.DataBean dataBean = (HeartData.DataBean) getIntent().getExtras().getSerializable("data");
        renderingUI(dataBean);
    }

    private void initTop() {
        signTopbar.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
            @Override
            public void leftclick() {
                finish();
            }

            @Override
            public void leftTextClick() {

            }

            @Override
            public void rightclick() {

            }

            @Override
            public void rightTextClick() {

            }
        });
    }

    public void getData() {
        userinfoModel = UserInfoDao.getInstance().queryUserinfoModel();
        if (userinfoModel != null) {
            Call<HeartData> heartData = ApiHelper.getHeartData(userinfoModel.getId(), userinfoModel.getAndroidtoken());
            heartData.enqueue(new Callback<HeartData>() {
                @Override
                public void onResponse(Call<HeartData> call, Response<HeartData> response) {
                    final HeartData body = response.body();
                    if (body.getCode().equals("0")) {
                        renderingUI(body.getData());
                    } else if (body.getCode().equals("1")) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                progressbar.setVisibility(View.GONE);
                                Toast.makeText(IndividualHeartActivity.this, body.getMsg(), Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                }

                @Override
                public void onFailure(Call<HeartData> call, Throwable t) {
                    Log.d("IndividualHeartActivity", "fial");
                }
            });
        } else {
            Toast.makeText(this, "用户数据异常", Toast.LENGTH_SHORT).show();
        }
    }

    private void renderingUI(HeartData.DataBean data) {
        progressbar.setVisibility(View.GONE);
        List<HeartData.DataBean.ListBean> list = data.getList();
        HeartData.DataBean.ListBean listBean = list.get(0);
        dateValue.setText(listBean.getShowtime());
        refreshUI(listBean);
        dataSize = list.size();
        if (dataSize > 3) {
            rvDate.setLayoutManager(new LinearLayoutManager(this));
            rvDate.setAdapter(new HeartAdapter(this, data));
        } else {
            rvDateTwo.setLayoutManager(new LinearLayoutManager(this));
            rvDateTwo.setAdapter(new HeartAdapter(this, data));
        }
    }

    public void refreshUI(HeartData.DataBean.ListBean listBean) {
        dateValue.setText(listBean.getShowtime());
        btnMore.setBackground(getResources().getDrawable(R.mipmap.down_btn));
        progressbar.setVisibility(View.VISIBLE);
        userinfoModel = UserInfoDao.getInstance().queryUserinfoModel();
        if (userinfoModel != null) {
            Call<HeartDetailData> heartDetailData = ApiHelper.getHeartDetailData(listBean.getCreate_time(), userinfoModel.getId(), userinfoModel.getAndroidtoken());
            heartDetailData.enqueue(new Callback<HeartDetailData>() {
                @Override
                public void onResponse(Call<HeartDetailData> call, Response<HeartDetailData> response) {
                    final HeartDetailData body = response.body();
                    if (body == null) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(IndividualHeartActivity.this, getResources().getString(R.string.common_net_error), Toast.LENGTH_SHORT).show();
                                progressbar.setVisibility(View.GONE);
                            }
                        });
                        return;
                    }
                    if (body.getCode().equals("0")) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                infoLayout.setVisibility(View.VISIBLE);
                                progressbar.setVisibility(View.GONE);
                                HeartDetailData.DataBean data = body.getData();
                                tvDescribeValue.setText(data.getDesc());
                                tvCaseValue.setText(data.getDesc2());
                                modeValue.setText(data.getSoprt_type());
                                rhythmValue.setText(data.getHeart_rate());
                                completeValue.setText(data.getUse_time());
                                fvValue.setText(data.getFc_value());
                            }
                        });
                    } else {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                progressbar.setVisibility(View.GONE);
                                Toast.makeText(IndividualHeartActivity.this, body.getMsg(), Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                }

                @Override
                public void onFailure(Call<HeartDetailData> call, Throwable t) {

                }
            });
        } else {
            Toast.makeText(this, "网络错误", Toast.LENGTH_SHORT).show();
            progressbar.setVisibility(View.GONE);
        }
    }

    @OnClick(R.id.above_layout)
    public void onViewClicked() {
        refreshUi();
    }

    public void refreshUi(){
        if (dataSize > 3) {
            if (rvDate.getVisibility() == View.GONE) {
                rvDate.setVisibility(View.VISIBLE);
                btnMore.setBackground(getResources().getDrawable(R.mipmap.up_btn));
            } else {
                rvDate.setVisibility(View.GONE);
                btnMore.setBackground(getResources().getDrawable(R.mipmap.down_btn));
            }
        } else {
            if (rvDateTwo.getVisibility() == View.GONE) {
                rvDateTwo.setVisibility(View.VISIBLE);
                btnMore.setBackground(getResources().getDrawable(R.mipmap.up_btn));
            } else {
                rvDateTwo.setVisibility(View.GONE);
                btnMore.setBackground(getResources().getDrawable(R.mipmap.down_btn));
            }
        }
    }
}
